/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package besa_adaptado.guardas.manejadorperfiles;


import adaptation.common.ProfileAES;
import adaptation.common.filter.FilterAES;
import adaptation.profilemanager.GuardRecieveFilterQuery;
import adaptation.profilemanager.ProfileManagerStateAES;
import adaptation.profilemanager.persistence.FacadeProfileManagerPersistence;
import besa_adaptado.PanelConsulta;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ayllu
 * Esta clase extiende de la guarda GuardRecieveFilterQuery, esta guarda debe ser imlementada para el funcionamiento de AES
 * Debe implementar el metodo obtainProfiles el cual, a través de un filtro de entrada, obtiene los perfiles correspondientes
 * En este ejemplo los obtiene de archivos binarios a través de la FacadeProfileManagerPersistence
 */
public class GuardReceiveFilter extends GuardRecieveFilterQuery {
    @Override
    public List<ProfileAES> obtainProfiles(FilterAES filter) {
        List<ProfileAES> resp = new ArrayList<ProfileAES>();
        FacadeProfileManagerPersistence facade = new FacadeProfileManagerPersistence();
        ProfileManagerStateAES state = (ProfileManagerStateAES) this.getAgent().getState();
        for (ProfileAES profile : state.getNewContext()) {
            for (ProfileAES stored : facade.getProfileList()) {
                if (stored.getCategory().equals(filter.getCategory())&&profile.getCategory().equals(filter.getCategory())) {
                    Field[] fields = profile.getClass().getDeclaredFields();
                    for (Field field : fields) {
                        try {
                            field.setAccessible(true);
                            if (field.get(stored).equals(field.get(profile))) {
                                resp.add(stored);
                                
                                PanelConsulta panel = (PanelConsulta) PanelConsulta.getInstance();
                                if(stored.getCategory().equals("device")){
                                    panel.getJtaPerfilDispositivo().setText(stored.toString());
                                }else if(stored.getCategory().equals("user")){
                                    panel.getJtaPerfilUsuario().setText(stored.toString());
                                }else if(stored.getCategory().equals("location")){
                                    panel.getJtaPerfilLocalizacion().setText(stored.toString());
                                }
                            }
                        } catch (IllegalArgumentException ex) {
                            Logger.getLogger(GuardReceiveFilter.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IllegalAccessException ex) {
                            Logger.getLogger(GuardReceiveFilter.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }

        }
        return resp;
    }   
}
